package com.guigu.pd.work.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.serviceapi.pojo.OrderEntity;
import com.guigu.pd.work.common.CustomIdGenerator;
import com.guigu.pd.oms.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.guigu.pd.work.entity.TransportOrderEntity;
import com.guigu.pd.work.service.TransportOrderService;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.R;



/**
 * 运单表
 *
 * @author deyran
 * @email deyran@gmail.com
 * @date 2022-07-19 19:31:34
 */
@RestController
@RequestMapping("work/transportorder")
public class TransportOrderController {
    @Autowired
    private TransportOrderService transportOrderService;

    @Autowired
    private CustomIdGenerator customIdGenerator;



    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) throws ParseException {
        PageVo<TransportOrderEntity> page = transportOrderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
        TransportOrderEntity transportOrder = transportOrderService.getinfobyId(id);

        return R.ok().put("transportOrder", transportOrder).put("order",transportOrder.getOrderinfo()).put("ordercargo",
                transportOrder.getOrdercargo()).put("taskpd",transportOrder.getTaskpt());
    }

    @GetMapping("/gettransportbyorderid/{id}")
    public String getTransportbyorderId(@PathVariable("id")String id){
        QueryWrapper<TransportOrderEntity> queryWrapper = new QueryWrapper<TransportOrderEntity>();
        queryWrapper.eq("order_id",id);
        TransportOrderEntity transportOrder = transportOrderService.getOne(queryWrapper);
        if(transportOrder!=null){
            return transportOrder.getId();
        }else{
            return null;
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TransportOrderEntity transportOrder){
		transportOrderService.save(transportOrder);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TransportOrderEntity transportOrder){
		transportOrderService.updateById(transportOrder);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete/{ids}")
    public R delete(@PathVariable(name = "ids")String[] ids){
        for (String id : ids) {
            transportOrderService.removeById(id);
        }

        return R.ok();
    }

    @RequestMapping("/addtransportorder/{id}")
    public Boolean addtransportorder(@PathVariable("id") String id) throws ParseException {
        TransportOrderEntity transportOrder = new TransportOrderEntity();
        transportOrder.setOrderId(id);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Date date = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss").parse(format);
        transportOrder.setCreateTime(date);
        String toid = customIdGenerator.nextId(transportOrder)+"";
        transportOrder.setId(toid);
        boolean save = transportOrderService.save(transportOrder);

        return save;
    }


    @GetMapping("/updatestatus/{id}")
    Boolean updatestatus(@PathVariable("id") String id){
        LambdaUpdateWrapper<TransportOrderEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TransportOrderEntity::getId,id).set(TransportOrderEntity::getStatus,3);
        boolean update = transportOrderService.update(lambdaUpdateWrapper);
        return update;
    }

}
